package aapilast1;

public class SyncDemo2 {
    public static void main(String[] args) {
        Shop shop = new Shop();
        Thread t1 = new Thread(shop::buy);
        Thread t2 = new Thread(shop::buy);
        t1.start();
        t2.start();
    }
}

class Shop {
    // public synchronized void buy()
    public void buy() {
        Thread t = Thread.currentThread();
        try {
            System.out.println(t.getName() + "正在买商品...");
            Thread.sleep(2000);
            synchronized (this) {
                System.out.println(t.getName() + "正在结账...");
                Thread.sleep(2000);
                System.out.println(t.getName() + "离开...");
                Thread.sleep(2000);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}